import { CommonTable } from "@/pages/OrderModule/common/components/CommonTable";
import SubTitle from "@/pages/OrderModule/common/components/SubTitle";
import { Alert, Button, Checkbox, Radio, Space, Switch, message } from "antd";
import { useState, useEffect } from "react";
import { getMergeConfigOp, updateMergeConfigOp, ecStoreListPageOp, updateMergeOrderStatusOp } from "../../service";
import { cloneDeep } from "lodash";

const AutoMergeOrder = ({ tab }) => {
	const [mergeConfigData, setMergeConfigData] = useState({}); //查询订单合并配置
	const [radioValue1, setRadioValue1] = useState(0); //主订单
	const [checkedValues, setCheckedValues] = useState([]); //其他规则选中数组
	const [tableData, setTableData] = useState([]); //表格数据

	//	主订单策略
	const { mainOrderRule, spitOrder, mergeOrder, replaceOrder, differentWarehouseOrder } = mergeConfigData;

	useEffect(() => {
		if (tab == 2) {
			getMergeConfig();
			getEcStoreListPageOp();
		}
	}, [tab]);

	useEffect(() => {
		const arr = cloneDeep(checkedValues);
		setRadioValue1(mainOrderRule);
		if (spitOrder) {
			arr.push("spitOrder");
		}
		if (mergeOrder) {
			arr.push("mergeOrder");
		}
		if (replaceOrder) {
			arr.push("replaceOrder");
		}
		if (differentWarehouseOrder) {
			arr.push("differentWarehouseOrder");
		}
		setCheckedValues(arr);
	}, [mergeConfigData]);

	//查询店铺表格数据
	const getEcStoreListPageOp = async () => {
		window.START_LOADING();
		const { success, data = {} } = await ecStoreListPageOp({
			current: 1,
			size: 500
		});
		if (success) {
			const { records = [] } = data;
			setTableData(records);
		}
		window.END_LOADING();
	};

	//查询订单合并配置
	const getMergeConfig = async () => {
		window.START_LOADING();
		const { success, data = {} } = await getMergeConfigOp();
		if (success) {
			setMergeConfigData(data);
		}
		window.END_LOADING();
	};

	//修改订单基础配置数据
	const updateMergeOrderStatus = async (val, record) => {
		window.START_LOADING();
		const { success, msg = "" } = await updateMergeOrderStatusOp({
			id: record.id,
			mergeOrderStatus: val ? 1 : 0
		});
		if (success) {
			message.success(msg);
			getEcStoreListPageOp();
		}
		window.END_LOADING();
	};

	//更新店铺自动合单状态
	const updateMergeConfig = async () => {
		window.START_LOADING();
		const { success, msg = "" } = await updateMergeConfigOp({
			id: mergeConfigData.id,
			mainOrderRule: radioValue1,
			spitOrder: checkedValues.includes("spitOrder") ? true : false,
			mergeOrder: checkedValues.includes("mergeOrder") ? true : false,
			replaceOrder: checkedValues.includes("replaceOrder") ? true : false,
			differentWarehouseOrder: checkedValues.includes("differentWarehouseOrder") ? true : false
		});
		if (success) {
			message.success(msg);
			getMergeConfig();
		}
		window.END_LOADING();
	};

	//多选选择
	const onCheckboxChange = val => {
		setCheckedValues(val);
	};

	const renderAlertMsg = () => {
		return (
			<div>
				<div>1、只有待审核、异常状态的订单才会参与自动合单；</div>
				<div>2、不同店铺的订单无法自动合单；</div>
				<div>3、同一平台，同一店铺、同一买家账号的订单；订单收货人，手机，收货地址，买家昵称完全一致。</div>
			</div>
		);
	};

	const tableColumns = [
		{
			title: "序号",
			dataIndex: "index",
			key: "index",
			width: 60,
			render: (t, r, i) => i + 1
		},
		{
			title: "店铺",
			dataIndex: "storeName"
		},
		{
			title: "平台",
			dataIndex: "typeName"
		},
		{
			title: "状态",
			dataIndex: "field4",
			render: (_, record) => {
				const nowTime = Date.now();
				//判断店铺是否过期，true: 没过期
				const isExprie = nowTime < record.authExpireTime * 1000;
				if (record.type == 0) {
					return "正常";
				} else {
					if (isExprie) {
						return "已授权";
					} else {
						return "授权过期";
					}
				}
			}
		},
		{
			title: "是否合单",
			dataIndex: "isMergeOrder",
			render: (_, record) => {
				const nowTime = Date.now();
				//判断店铺是否过期，true: 没过期
				const isExprie = nowTime < record.authExpireTime * 1000;
				if (record.type == 0) {
					return (
						<Switch
							checkedChildren="已开启"
							unCheckedChildren="已关闭"
							defaultChecked={record.isMergeOrder}
							onChange={val => {
								updateMergeOrderStatus(val, record);
							}}
						/>
					);
				} else {
					if (!isExprie || record.type == 20) {
						return <Switch checkedChildren="已开启" unCheckedChildren="已关闭" defaultChecked={false} disabled />;
					} else {
						return (
							<Switch
								checkedChildren="已开启"
								unCheckedChildren="已关闭"
								defaultChecked={record.isMergeOrder}
								onChange={val => {
									updateMergeOrderStatus(val, record);
								}}
							/>
						);
					}
				}
			}
		}
	];

	//其他规则
	const otherRuleOptions = [
		{ label: "允许合并有拆分标记的订单", value: "spitOrder" },
		{ label: "允许合并有合并标记的订单", value: "mergeOrder" },
		{ label: "允许合并补发换货的订单", value: "replaceOrder" },
		{ label: "允许合并不同发货仓库的订单", value: "differentWarehouseOrder" }
	];

	return (
		<Space direction="vertical" size={20}>
			<SubTitle title="合单规则" as="s2" />
			<Alert message={renderAlertMsg()} type="info" showIcon />
			<CommonTable columns={tableColumns} dataSource={tableData} pagination={false} scroll={{ y: 200 }} rowKey={"id"} />
			<div style={{ maxHeight: 45 }}>
				<SubTitle title="其他规则" as="s2" />
				<Checkbox.Group value={checkedValues} onChange={onCheckboxChange}>
					{/* <Space size={70}> */}
					{otherRuleOptions.map(item => (
						<Checkbox key={item.label} value={item.value}>
							{item.label}
						</Checkbox>
					))}
					{/* </Space> */}
				</Checkbox.Group>
			</div>
			<div>
				<SubTitle title="主订单" as="s2" />
				<Radio.Group onChange={e => setRadioValue1(e.target.value)} value={radioValue1}>
					{/* <Space size={40}> */}
					<Radio value={1}>以最早付款的订单作为主订单</Radio>
					<Radio value={2}>以最晚付款的订单作为主订单</Radio>
					{/* </Space> */}
				</Radio.Group>
			</div>
			<div>
				<Button onClick={updateMergeConfig} type="primary">
					保存
				</Button>
			</div>
		</Space>
	);
};
export default AutoMergeOrder;
